<?php
/**
 * 轮播控制器
 */

namespace app\admin\controller;

use think\Request;
use app\common\model\Slide;

use app\common\validate\SlideValidate;

class SlideController extends Controller
{

    //列表
    public function index(Request $request, Slide $model)
    {
        if ($this->request->isAjax()) {
            $get = $this->request->get();
            
            $lists=Slide::lists($get);
            return json([
                'code' =>1,
                'msg'  =>'',
                'data' =>$lists,
            ]);
        }
        return $this->fetch();
    }

    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $post['delete_time '] = 0;
            $result = $this->validate($post, 'app\admin\validate\Slide.add');
            if ($result === true) {
                $result = Slide::add($post);
                if ($result) {
                    return success('添加成功！');
                } else {
                    return error('添加失败');
                }
            }
            return error($result);
        }
        $this->assign('link_page', \app\common\model\Slide::getLinkPage(1));
        $this->assign('position_list', Slide::infoPosition());
        return $this->fetch();
    }

    /**
     * 编辑
     * @param string $id
     * @return mixed
     */
    public function edit($id = '')
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $post['del'] = 0;
            $result = $this->validate($post, 'app\admin\validate\Slide.add');
            if ($result === true) {
                $result = Slide::edit($post);
                if ($result) {
                    return success('编辑成功！');
                } else {
                    return error('编辑失败');
                }
            }
            return error($result);
        }
        $info = Slide::info($id);
        $this->assign('info',$info);
        $this->assign('link_page', \app\common\model\Slide::getLinkPage(1));
        $this->assign('position_list', Slide::infoPosition());
        return $this->fetch();
    }


    /**
     * 删除
     * @param $delData
     */
    public function del($id, $client)
    {
        if ($this->request->isAjax()) {
            $result = Slide::del($id, $client);
            if ($result) {
                return success('删除成功');
            }
            return error('删除失败');
        }
    }


    /**
     * 修改状态
     */
    public function switchStatus()
    {
        $post = $this->request->post();
        $result = Slide::switchStatus($post);
        if ($result) {
            return success('修改成功');
        }
        return error('修改失败');
    }
}
